package com.sgb.kjwl.view.widget;

import android.content.Context;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsTypeAdapter3;
import com.sgb.kjwl.view.ui.adapter.transport.TransportLoadGoodsTimeAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.TransportAdapterOnItemClick;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class LoadTimePopWindow extends BasePopWindow {

    private TransportLoadGoodsTimeAdapter3 timeAdapter;

    private List<GoodsInfoTypeEntity3> loadGoodsTime;

    private GoodsInfoTypeEntity3 currentItem;

    public LoadTimePopWindow(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_goods_types;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        initTimeData();
        RecyclerView recyclerView = holder.getPop().findViewById(R.id.type_list);

        //装货时间
        timeAdapter = new TransportLoadGoodsTimeAdapter3(mContext, loadGoodsTime);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(timeAdapter);
        timeAdapter.setDefItem(0);
        timeAdapter.setItemSelect(true);

        timeAdapter.setOnItemClick(new TransportAdapterOnItemClick<GoodsInfoTypeEntity3>() {
            @Override
            public void onItemClick(GoodsInfoTypeEntity3 infoTypeEntity, int position) {
                onResultClick.result(infoTypeEntity, position, null);
                currentItem = infoTypeEntity;
                dissMiss();
            }
        });
    }

    private void initTimeData() {

        loadGoodsTime = new ArrayList<>();
        loadGoodsTime.add(new GoodsInfoTypeEntity3("0", "不限"));

        Date now = new Date();
        Calendar nowCal = Calendar.getInstance();
        nowCal.setTime(now);
        nowCal.set(Calendar.HOUR_OF_DAY, 23);
        nowCal.set(Calendar.MINUTE, 59);
        nowCal.set(Calendar.SECOND, 59);
        nowCal.set(Calendar.MILLISECOND, 0);
        loadGoodsTime.add(new GoodsInfoTypeEntity3(nowCal.getTime().getTime() + "", "今天"));


        Calendar tomorrow = Calendar.getInstance();
        tomorrow.setTime(now);
        tomorrow.add(Calendar.DATE, 1);
        tomorrow.set(Calendar.HOUR_OF_DAY, 23);
        tomorrow.set(Calendar.MINUTE, 59);
        tomorrow.set(Calendar.SECOND, 59);
        tomorrow.set(Calendar.MILLISECOND, 0);
        loadGoodsTime.add(new GoodsInfoTypeEntity3(tomorrow.getTime().getTime() + "", "明天"));

        Calendar later = Calendar.getInstance();
        later.setTime(now);
        later.add(Calendar.DATE, 2);
        later.set(Calendar.HOUR_OF_DAY, 0);
        later.set(Calendar.MINUTE, 0);
        later.set(Calendar.SECOND, 0);
        later.set(Calendar.MILLISECOND, 0);
        loadGoodsTime.add(new GoodsInfoTypeEntity3(later.getTime().getTime() + "", "明天以后"));
    }


    public void setDefItem(int position) {
        timeAdapter.setDefItem(position);
        if (loadGoodsTime != null && loadGoodsTime.size() > position)
            currentItem = loadGoodsTime.get(position);
    }

    public boolean isSetLoadTime() {
        return currentItem != null;
    }

    public String getLoadTimeStr() {
        return currentItem.getName();
    }

    public void resetSelectItem() {
        timeAdapter.setDefItem(0);
        timeAdapter.resetCheckDatas();
    }


}
